package com.tarry.common;

import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

/**
 * JSON数据返回实体
 */
@Getter
@Setter
public class Result {
    private boolean ret;
    private String msg;
    private Object data;

    protected Result(boolean result){
        this.ret = result;
    }

    public static Result success(){
        Result result = new Result(true);
        return result;
    }

    /**
     * 请求成功
     * @param data 返回数据
     * @return
     */
    public static Result success(Object data){
        Result result = new Result(true);
        result.data = data;
        return result;
    }

    /**
     * 请求成功
     * @param data 返回数据
     * @param msg 返回消息
     * @return
     */
    public static Result success(Object data,String msg){
        Result result = new Result(true);
        result.data = data;
        result.msg = msg;
        return result;
    }

    /**
     * 请求失败
     * @param msg 返回消息
     * @return
     */
    public static Result error(String msg){
        Result result = new Result(false);
        result.msg = msg;
        return result;
    }

    public static Map<String,Object> toMap(Result result){
        Map map = new HashMap();
        map.put("result",result.ret);
        map.put("data",result.data);
        map.put("msg",result.msg);
        return  map;
    }
}
